<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
	String path = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath()
			+ "/";
	request.setAttribute("path", path);
%>
<base href="<%=path%>"></base>
<title>缺陷管理</title>
<link rel="stylesheet" type="text/css"
	href="jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="jquery-easyui/themes/icon.css">
<script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui/jquery.easyui.min.js"></script>
</head>
<body>
	<!-- datagrid 显示数据 -->
	<table id="dg" style="width: 1300px; height: 250px"
		data-options="rownumbers:true,singleSelect:true,pagination:true,method:'get'">
		<thead>
			<tr>
				<th data-options="field:'configvaluename',width:150">缺陷类型名称</th>
				<th
					data-options="field:'isstart',width:150,align:'right',formatter:changestate">
					状态（启用/未启用）</th>
				<th data-options="field:'op',width:250,align:'center',formatter:op">操作</th>
			</tr>
		</thead>
	</table>
	<!-- 辅助的小按钮 -->
	<div id="toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="addConfig()">New Bug</a> 
	</div>
	<!--修改小窗口  -->
	<div id="add_updateDD" class="easyui-dialog" title="修改/新增缺陷"
		style="width: 400px; height: 200px;"
		data-options="iconCls:'icon-save',resizable:true,modal:true,closed: true">
		<form id="aad_updateFF" method="post">
			<div>
				<label>缺陷类型名称</label><s:textfield class="easyui-validatebox" name="psConfig.configvaluename"
					data-options="required:true,validType:'isExist'"
					label="缺陷类型名称" id="configvaluename" />
			</div>
			<div>
				<label>启用状态</label><s:radio list="#{'1':'启用','-1':'停用' }" name="psConfig.isstart"
					label="启用状态"></s:radio>
			</div>
		</form>
	</div>
	<script type="text/javascript">
	//修改缺陷状态
	function toggleConfig() {
		var row=$('#dg').datagrid('getSelected');
		var id=row.id;
		$.ajax({
			url : 'config/toggleConfig',
			data : {
				'psConfig.id' : id
			},
			dataType : 'json',
			success : function(data) {
				showMesage(data.message);
				$('#dg').datagrid('reload');
			}
		}); 
	}
	//新增
	function addConfig() {
		$('#add_updateDD').dialog({
			buttons : [ {
				text : 'Cancel',
				handler : function() {
					$('#add_updateDD').dialog('close');
				}
			}, {
				text : 'Save',
				iconCls : 'icon-save',
				handler : function() {
					//表单提交
					$('#aad_updateFF').form({
						url : 'config/addUpdateConfig',
						onSubmit : function(psPole) {
							$('#aad_updateFF').form('validate');
						},
						success : function(data) {
							var data = eval('(' + data + ')');
							showMesage(data.message);
							$('#dg').datagrid('reload');
						}
					}).submit();

				}
			} ]
		}).dialog('open');
	}
	//修改
	function updateConfig(id) {
		$.ajax({
			url : 'config/findConfig',
			data : {
				'psConfig.id' : id
			},
			dataType : 'json',
			success : function(data) {
				//表单数据填充
				$('#aad_updateFF').form('load', {
					'psConfig.configvaluename' : data.configvaluename,
					'psConfig.isstart' : data.isstart
				});
			}
		});
		$('#add_updateDD').dialog({
			buttons : [ {
				text : 'Cancel',
				handler : function() {
					$('#add_updateDD').dialog('close');
				}
			}, {
				text : 'Save',
				iconCls : 'icon-save',
				handler : function() {
					//表单提交
					$('#aad_updateFF').form({
						url : 'config/addUpdateConfig',
						onSubmit : function(psPole) {
							psPole['psConfig.id'] = id;
							$('#aad_updateFF').form('validate');
						},
						success : function(data) {
							var data = eval('(' + data + ')');
							showMesage(data.message);
							$('#dg').datagrid('reload');
						}
					}).submit();

				}
			} ]
		}).dialog('open');
	}
		//对停用、修改、删除操作格式化
		function op(val, row) {
			var id = row.id;
			return "<a href='javascript:void(0)' onclick='updateConfig("
					+ id
					+ ")'>修改|</a><a href='javascript:void(0)' onclick='deleteConfig("
					+ id + ")'>删除|</a>";
		}
		function showMesage(msg) {
			$.messager.show({
				title : '执行结果',
				msg : msg,
				showType : 'show'
			});
		}
		function deleteConfig(id) {
			$.ajax({
				url : 'config/deleteConfig',
				data : {
					'psConfig.id' : id
				},
				dataType : 'json',
				success : function(data) {
					showMesage(data.message);
					$('#dg').datagrid('reload');
				}
			});
		}
		
		
		function changestate(val, row) {
			 if (row.isstart == true) {
				return "<input checked='checked' type='checkbox' onchange='toggleConfig()'/>";
			} else {
				return "<input type='checkbox'  onchange='toggleConfig()'/>";
			} 
		}
		//初始化
		$(function() {
			$('#dg').datagrid({
				url : 'config/showConfig',
				toolbar:'#toolbar'
			});
			$.extend($.fn.validatebox.defaults.rules, {    
			    isExist: {    
			        validator: function(value, param){
			        	var d;
			        	$.ajax({
							url : 'config/findConfigValueName',
							async: false,//是否异步执行
							type:'post',
							data : {
								'psConfig.configvaluename' : value
							},
							dataType : 'json',
							success : function(data) {
								d=data;
							}
						});
			        	return !d.success;
			        },    
			        message: '已存在.'   
			    }    
			}); 
		})
	</script>
</body>
</html>